var dmall = require("../../utils/util.js")


// var app = getApp();
Page({
  data: {
    registBtnTxt: "注册",
    registBtnBgBgColor: "#0099FF",
    getSmsCodeBtnColor: "#0099FF",
    // getSmsCodeBtnTime:60,
    btnLoading: false,
    registDisabled: false,
    smsCodeDisabled: false,
    inputUserName: '',
    inputPassword: '',
    logIcon: "../../images/logI.png",
    pwdIcon: "../../images/pwdI.png",
    formData: null,
    imgUrl: ""
  },
  chooseimage: function () {
    dmall.chooseImg(this)
  },
  formSubmit: function (e) {
    var param = e.detail.value;
    this.setData({
      formData: param
    })
    this.mysubmit(param);
  },
  mysubmit: function (param) {
    var that = this
    console.log(param)
    var flag = dmall.checkPhoneNum(param.username, that) &&  dmall.checkPassword(param.password, that);
    var that = this;
    if (flag) {
      this.setregistData1();
      wx.request({
        url: 'http://1.aiym.applinzi.com/aiyouwai.php',
        data: {
          type: 'regist',
          paras: this.data.formData
        },
        header: { 'content-type': 'application/json' },
        method: 'GET',
        success: function (res) {
          // success
          console.log(res.data)
          if (res.data.errcode == 0) {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 500
            });
            that.setregistData2();
            that.redirectTo(param);
          } else if (res.data.errcode == 1) {
            wx.showModal({
              title: '提示',
              content: '该用户已存在',
              success: function () {
                that.redirectTo(param);
              }
            });
          }
        }
      })
    }
  },
  setregistData1: function () {
    this.setData({
      registBtnTxt: "注册中",
      registDisabled: !this.data.registDisabled,
      registBtnBgBgColor: "drakgray",
      btnLoading: !this.data.btnLoading
    });
  },
  setregistData2: function () {
    this.setData({
      registBtnTxt: "注册",
      registDisabled: !this.data.registDisabled,
      registBtnBgBgColor: "#0099FF",
      btnLoading: !this.data.btnLoading
    });
  },


  redirectTo: function (param) {
    //需要将param转换为字符串
    param = JSON.stringify(param);
    wx.redirectTo({
      url: '../login/login'  //参数只能是字符串形式，不能为json对象
    })
  }
})